package org.itboys.mobile.service.mongo.weixin;


import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.image.ImageUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.ZhongBaoCustomer;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCodeService;
import org.itboys.mobile.util.NumberUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：众包_客户_service
 */
@Service
public class ZhongBaoWeiXinCustomerService extends BaseMongoService<ZhongBaoCustomer> {

    @Autowired
    private SundayWeiXinMemberService memberService;
    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<ZhongBaoCustomer> getEntityClass() {
        return ZhongBaoCustomer.class;
    }


    /**
     * 新增客户
     * @param memberId
     * @param customerId
     * @param type //1，推荐，2项目关注，3项目报名。
     */
    public void addCustomer(Long memberId,Long customerId,Integer type){
        if(LongUtil.isNotZreo(customerId)){
            //2017年12月21日，校验用户和客户ID是否是同事关系
            SundayMember member=memberService.getById(memberId);
            SundayMember customer =memberService.getById(customerId);
            if(member.getParentId().longValue()==customer.getId()||
                    member.getId()==customer.getParentId().longValue()){
                return;
            }
           // SundayMember customer =memberService.getById(customerId);
            super.save(new ZhongBaoCustomer(memberId,customerId,type));
        }
    }

    /**
     * 获取客户
     * @param request
     * @param memberId
     * @return
     */
    public List<SundayMember> getCustomer(HttpServletRequest request,Long memberId){
        Map<String,Object> param = new HashMap<String ,Object>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("memberId",memberId);
        List<ZhongBaoCustomer> customers=super.list(param);
        Set<Long> memberIds = new HashSet<>();
        for(ZhongBaoCustomer customer:customers){
            memberIds.add(customer.getCustomerId());
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("id in",memberIds);
        List<SundayMember> members= memberService.list(param);
        for(ZhongBaoCustomer customer:customers){
            for(SundayMember member:members){
                if(LongUtil.isNotZreo(customer.getCustomerId())&&
                        customer.getCustomerId().longValue()==member.getId()){
                    customer.setCustomerName(member.getNickName());
                    customer.setCustomerImage(member.getImage());
                    customer.setCustomerMobile(member.getMobile());
                    break;
                }
            }
        }

        return members;
    }
}
